Skip to content

Conversation

@icweaver
Copy link
Member

Now that we are past supporting pre-v1.3 versions of Julia, are we ready to put this legendary comment to rest?

# --------------------------------------------------------------------------------
# Here be codegen!

# generate unitful support for the following laws
# this can be removed when julia support is pinned to 1.3 or higher,
# at which point adding `(l::ExtinctionLaw)(wave::Quantity)` is possible, until then
# using this code-gen does the trick but requires manually editing
# instead of providing support for all <: ExtinctionLaw

@codecov
Copy link

codecov bot commented Feb 14, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (d46b630) to head (6ef359a).
Report is 7 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master       #57   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           10        10           
  Lines          296       296           
=========================================
  Hits           296       296           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

(l::law)(wavelength::U.Quantity) = l(U.ustrip(U.u"Å", wavelength)) * U.u"mag"
end
# generate unitful support
(l::ExtinctionLaw)(wavelength::U.Quantity) = l(U.ustrip(U.u"Å", wavelength)) * U.u"mag"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're editing this I wonder if it makes sense to check dimensions here by dispatching as (l::ExtinctionLaw)(wavelength::U.Length) because wavelengths only make sense as lengths.

Copy link
Member Author

@icweaver icweaver May 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Making the switch seems to make the error message a bit more opaque though:

With (l::ExtinctionLaw)(wavelength::U.Quantity)

julia> CCM89()(1e4/5)
2.8425264357868345

julia> CCM89()(2000u"")
2.8425264357868345 mag

julia> CCM89()(2000u"kg")
ERROR: DimensionError: Å and kg are not dimensionally compatible.
Stacktrace:
 [1] #s103#141
   @ ~/.julia/packages/Unitful/nwwOk/src/conversion.jl:28 [inlined]
 [2] var"#s103#141"(::Any, s::Any, t::Any)
   @ Unitful ./none:0
 [3] (::Core.GeneratedFunctionStub)(::UInt64, ::LineNumberNode, ::Any, ::Vararg{Any})
   @ Core ./boot.jl:707
 [4] uconvert(a::Unitful.FreeUnits{(Å,), 𝐋, nothing}, x::Quantity{Int64, 𝐌, Unitful.FreeUnits{(kg,), 𝐌, nothing}})
   @ Unitful ~/.julia/packages/Unitful/nwwOk/src/conversion.jl:93
 [5] ustrip
   @ ~/.julia/packages/Unitful/nwwOk/src/utils.jl:37 [inlined]
 [6] (::CCM89)(wavelength::Quantity{Int64, 𝐌, Unitful.FreeUnits{(kg,), 𝐌, nothing}})
   @ DustExtinction ~/projects/DustExtinction.jl/src/DustExtinction.jl:167
 [7] top-level scope
   @ REPL[30]:1

With (l::ExtinctionLaw)(wavelength::U.Length)

julia> CCM89()(1e4/5)
2.8425264357868345

julia> CCM89()(2000u"")
2.8425264357868345 mag

julia> CCM89()(2000u"kg")
ERROR: MethodError: no method matching (::CCM89)(::Quantity{Int64, 𝐌, Unitful.FreeUnits{(kg,), 𝐌, nothing}})
The object of type `CCM89` exists, but no method is defined for this combination of argument types when trying to treat it as a callable object.

Closest candidates are:
  (::CCM89)(::T) where T<:Real
   @ DustExtinction ~/projects/DustExtinction.jl/src/color_laws/ccm89.jl:25
  (::DustExtinction.ExtinctionLaw)(::Union{Quantity{T, 𝐋, U}, Level{L, S, Quantity{T, 𝐋, U}} where {L, S}} where {T, U})
   @ DustExtinction ~/projects/DustExtinction.jl/src/DustExtinction.jl:167

Stacktrace:
 [1] top-level scope
   @ REPL[25]:1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I guess the type conversion ends up requiring a length anyway, nbd

@cgarling
Copy link
Member

cgarling commented May 8, 2025

Looks good to merge

@icweaver icweaver merged commit cf28a95 into JuliaAstro:master May 8, 2025
10 checks passed
@icweaver icweaver deleted the remove-codegen branch May 8, 2025 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants